package com.yumijinfu.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.view.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提现  充值 支付h5加载页面
 */
public class WebActivity extends BaseActivity {
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.pb_loading)
    ProgressBar pbLoading;
    @Bind(R.id.mWebView)
    WebView vWVContent;
    private String url;
    private String postData;
    private String params;
    private String titile_text;


    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_web);

        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        titile_text = getIntent().getStringExtra("titile_text");
        url = getIntent().getStringExtra("url").trim();
        postData = getIntent().getStringExtra("postData");
//        params = getIntent().getStringExtra("params");
        //
        titlebar.setTitleBar(R.drawable.back_selector, titile_text, 0);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            vWVContent.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); }

        vWVContent.setWebViewClient(new WebViewClient());
        vWVContent.getSettings().setJavaScriptEnabled(true);
        //js和android做交互
        vWVContent.addJavascriptInterface(new MyObject(), "object");
        vWVContent.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
        vWVContent.getSettings().setSupportZoom(true); //可以缩放
        //屏幕自适应
        vWVContent.getSettings().setUseWideViewPort(true);
        vWVContent.getSettings().setLoadWithOverviewMode(true);
        vWVContent.setWebChromeClient(new WebChromeClient());

        vWVContent.setWebViewClient(new WebViewClient() {


            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                super.onReceivedSslError(view, handler, error);
                // TODO Auto-generated method stub
                //允许https://的访问
                //Android使用WebView加载https地址打不开的问题  小米
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //加载完成
//                Toast.makeText(WebActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //加载开始
//                Toast.makeText(WebActivity.this, "加载开始", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                //加载失败
//                Toast.makeText(WebActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
            }
        });

        vWVContent.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                pbLoading.setProgress(newProgress);
                if (newProgress == 100) {
                    pbLoading.setVisibility(View.GONE);
                }
            }
        });

//        vWVContent.postUrl(url, params);
        vWVContent.loadData(postData,"text/html", "UTF-8");

    }


    //Android 4.2以上系统，通过在Java的远程方法上面声明
    // @JavascriptInterface可以解决WebView漏洞。如下面代码：
    public class MyObject {
        //        @JavascriptInterface一定的加上
        @JavascriptInterface
        public void back() {   //提现成功
            startActivity(new Intent(WebActivity.this, TradingRecordActivity.class));
            finish();
        }

        @JavascriptInterface
        public void Rback(){     //注册失败
//            Toast.makeText(WebActivity.this, "ssdfd", Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(WebActivity.this, TradingRecordActivity.class));
            finish();

        }







    }


    @OnClick({R.id.titlebar_iv_back})
    @Override
    protected void widgetClick(View view) {

        switch (view.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;

            default:
        }
    }
}
